//
//  SWVisitorLoginView.swift
//  SinaWeibo
//
//  Created by AenyMo on 16/3/30.
//  Copyright © 2016年 AenyMo. All rights reserved.
//

import UIKit

protocol SWVisitorLoginViewDelegate: NSObjectProtocol {
    //登录
    func visitorLogin()
    //注册
    func visitorRegister()
}

class SWVisitorLoginView: UIView {
    
    //MARK: - 代理事件
    //要使用 weak 属性, 必须继承 NSObjectProtocol 协议
    weak var delegate: SWVisitorLoginViewDelegate?
    
    //登录
    @objc private func loginClick() {
        
        delegate?.visitorLogin()
    }
    //注册
    @objc private func registerClick() {
        
        delegate?.visitorRegister()
    }
    
    //MARK: - UIInfo
    //公有方法,根据外界传的不同参数,加载不同的视图
    func setUIInfo(imageName: String?, title: String?) {
        //
        if imageName != nil {
            //设置
            circleView.image = UIImage(named: imageName!)
            tipLabel.text = title!
            //隐藏中间icon
            iconView.hidden = true
        } else {
            iconView.hidden = false
            //添加动画
            setAnimation()
        }
    }
    
    //MARK: - 首页动画
    private func setAnimation() {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        ani.duration = 20
        //当动画执行完 或 视图处于非活跃状态 动画默认停止
        ani.removedOnCompletion = false
        //添加动画
        circleView.layer.addAnimation(ani, forKey: nil)
    }

    //MARK: - 初始化
    init() {
        super.init(frame: CGRectZero)
        
        backgroundColor = UIColor.init(white: 0.93, alpha: 1.0)
        
        //添加UI
        setupUI();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置UI
    private func setupUI() {
        
        //添加子控件
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //VFL设置约束(布局)
        //1.首先要禁用AutoResizing
        for view in subviews {
            //
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //2.添加约束
        //圆圈控件
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //中间图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //建议文案
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        tipLabel.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        
        //登录
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: registerButton, attribute: .Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .RightMargin, relatedBy: .Equal, toItem: registerButton, attribute: .LeftMargin, multiplier: 1.0, constant: -40))

        //注册
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: loginButton, attribute: .Width, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .LeftMargin, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60))
        
        //阴影
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .Bottom, relatedBy: .Equal, toItem: registerButton, attribute: .Bottom, multiplier: 1.0, constant: 30))
        
    }
    
    //MARK: - 懒加载UI子控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var tipLabel: UILabel = {
        let tipLabel = UILabel();
        
        tipLabel.text = "关注一些人,回这里看看有什么惊喜,你的微薄将显示在这里"
        tipLabel.textAlignment = NSTextAlignment.Center
        tipLabel.font = UIFont.systemFontOfSize(14)
        tipLabel.textColor = UIColor.lightGrayColor()
        
        tipLabel.numberOfLines = 0
        
        tipLabel.sizeToFit()
        
        return tipLabel
    }()
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        btn.addTarget(self, action: #selector(loginClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        btn.addTarget(self, action: #selector(registerClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()

}
